package com.xiangban.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.dao.AccompanyMapper;
import com.xiangban.dao.AccompanyWalletMapper;
import com.xiangban.exception.BizException;
import com.xiangban.model.AccompanyInvite;
import com.xiangban.model.AccompanyWallet;
import com.xiangban.model.UserAccompany;
import com.xiangban.model.dto.UserAccompanyDto;
import com.xiangban.model.res.ApiResultCode;
import com.xiangban.model.res.Ids;
import com.xiangban.service.AccompanyService;
import com.xiangban.utils.CommonUtils;
import com.xiangban.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

import static cn.hutool.core.util.IdcardUtil.getAgeByIdCard;
import static com.xiangban.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/12/2 15:15
 */
@Service
public class AccompanyServiceImpl implements AccompanyService {

    @Autowired
    private AccompanyMapper accompanyMapper;
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private AccompanyWalletMapper accompanyWalletMapper;
    @Autowired
    private CommonUtils commonUtils;

    @Override
    public void AuditAccompany(UserAccompany userAccompany) {
        if (BizEmpty(userAccompany.getAccompanyId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(userAccompany.getStatus())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(userAccompany.getUid())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        accompanyMapper.updateAccompanyStatusByAccompanyId(userAccompany);
        //初始化陪诊钱包
        if (userAccompany.getStatus() == 0){
            AccompanyWallet accompanyWallet1 = accompanyWalletMapper.getAccompanyWallet(userAccompany.getAccompanyId());
            if (accompanyWallet1 == null){
                AccompanyWallet accompanyWallet = new AccompanyWallet();
                accompanyWallet.setAccompanyId(userAccompany.getAccompanyId());
                accompanyWallet.setMoney(new BigDecimal(0.00));
                accompanyWalletMapper.initAccompanyWallet(accompanyWallet);
            }

            //判断是否为邀请用户更改用户邀请状态
            AccompanyInvite accompanyInvite = new AccompanyInvite();
            accompanyInvite.setUid(userAccompany.getUid());
            AccompanyInvite accompanyInvite1 = accompanyMapper.getAccompanyInvite(accompanyInvite);
            if (accompanyInvite1 != null){
                accompanyInvite.setStatus(0);
                accompanyMapper.updateInviteStatus(accompanyInvite);
            }
        }
    }

    @Override
    public PageInfo<UserAccompanyDto> listAccompanyPage(UserAccompanyDto userAccompanyDto) {
        PageHelper.startPage(pageUtil.getPage().getPageNum(),pageUtil.getPage().getPageSize());
        List<UserAccompanyDto> userAccompanyDtos = accompanyMapper.listAccompany(userAccompanyDto);
        for (UserAccompanyDto accompanyDto : userAccompanyDtos) {
            int age = commonUtils.getAgeByIdCard(accompanyDto.getIdCard());
            accompanyDto.setAge(age);
        }
        return new PageInfo<>(userAccompanyDtos);
    }

    @Override
    public void updateAccompany(UserAccompanyDto userAccompanyDto) {
        if (BizEmpty(userAccompanyDto.getAccompanyId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
//        if ((userAccompanyDto.getIdCard() != null && !userAccompanyDto.getIdCard().equals("")) ||
//                (userAccompanyDto.getName() != null && !userAccompanyDto.getName().equals("")) ||
//                (userAccompanyDto.getPhone() != null && !userAccompanyDto.getPhone().equals("")) ||
//                userAccompanyDto.getSex() != null){
            accompanyMapper.updateAccompanyByAccompanyId(userAccompanyDto);
//        }else if (userAccompanyDto.getWorkImage() != null && !userAccompanyDto.getWorkImage().equals("")){
            accompanyMapper.updateAccompanyCertificateByAccompanyId(userAccompanyDto);
//        }

    }

    @Override
    public Object getAccompany(UserAccompany userAccompany) {
        if (BizEmpty(userAccompany.getAccompanyId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        UserAccompanyDto accompany = accompanyMapper.getAccompany(userAccompany);
        accompany.setAge(commonUtils.getAgeByIdCard(accompany.getIdCard()));
        return accompany;
    }

    @Override
    public void removeAccompany(Ids ids) {
        if (BizEmpty(ids.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(ids.getValue())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        accompanyMapper.deleteAccompany(ids);
    }
}
